package com.fpt.edu.vn.h2tv.api.pub;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import com.fpt.edu.vn.h2tv.common.ElementName;
import com.fpt.edu.vn.h2tv.dto.BookSample;
import com.fpt.edu.vn.h2tv.dto.UserInfo;
import com.fpt.edu.vn.h2tv.service.BookDetailService;
import com.fpt.edu.vn.h2tv.service.BookService;
import com.fpt.edu.vn.h2tv.service.CategoryService;
import com.fpt.edu.vn.h2tv.service.IBaseService;

@Path("/book/store")
public class BookStoreAPI {

	@Path("/sample")
	@Produces(MediaType.APPLICATION_XML)
	@GET
	public BookSample getSample() {
		BookSample sample = new BookSample();
		sample.setBookDescription("description");
		sample.setBookName("bookname");

		return sample;
	}

	@Path("/category")
	@GET
	public Response getCategoryList() {
		IBaseService service = new CategoryService();
		return buildResponse(Status.OK, MediaType.APPLICATION_XML,
				service.getResponse());
	}

	@Path("/allbook")
	@Produces(MediaType.APPLICATION_XML)
	@GET
	public Response getBookList(@QueryParam(value = "start") int start,
			@QueryParam(value = "limit") int limit,
			@QueryParam(value = "categoryId") int categoryId,
			@QueryParam(value = "keyWord") String keyWord) {
		return buildResponse(Status.OK, MediaType.APPLICATION_XML,
				BookService.getAllBookReponse(start, limit, categoryId, keyWord));
	}

	@Path("/hotbook")
	@Produces(MediaType.APPLICATION_XML)
	@GET
	public Response getHotBook() {
		return buildResponse(Status.OK, MediaType.APPLICATION_XML,
				BookService.findHotBook());
	}

	@Path("/detail")
	@GET
	public Response getProductDetail(@QueryParam(value = "bookID") int bookID) {
		return buildResponse(Status.OK, MediaType.APPLICATION_XML,
				BookDetailService.getBookDetail(bookID));
	}

	@Path("/comment/add")
	@PUT
	public Response addComment(@Context HttpServletRequest request,
			@FormParam(value = "bookID") int bookID,
			@FormParam(value = "comment") String comment) {
		UserInfo info = (UserInfo) request.getSession().getAttribute(
				ElementName.USER_INFO);
		if (info == null) {
			return buildResponse(Status.UNAUTHORIZED, MediaType.TEXT_PLAIN,
					"YOU HAVE NO PERMISSION TO ACCESS THIS SERVICE!!!");
		} else {
			return buildResponse(Status.OK, MediaType.APPLICATION_XML,
					BookDetailService.addComment(bookID, info.getUserId()
							.intValue(), comment));
		}
	}

	@Path("/comment/add")
	@OPTIONS
	public Response test() {
		return buildResponse(Status.OK, MediaType.TEXT_PLAIN, "ok");

	}

	private Response buildResponse(Status status, String mediaType,
			Object repsonse) {
		return Response.ok(repsonse, mediaType).status(status)
		// .header("Access-Control-Allow-Origin", "http://localhost")
		// .header("Access-Control-Allow-Methods",
		// "GET,POST,PUT,DELETE,OPTIONS,TRACE,HEAD,CONNECT")
		// .header("Access-Control-Allow-Credentials",true)
		// .header("Access-Control-Allow-Headers",
		// "x-accept-charset, x-accept, origin,X-Accept-Charset,X-Accept,Content-Type,Authorization,REFERER")
				.build();
	}

}
